package com.superteam.zzl.activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.KeyEvent;
import android.view.View;

import android.widget.RadioGroup;

import com.superteam.zzl.activity.databinding.ActivityMainBinding;
import com.superteam.zzl.utils.NavigationAnimationUtils;
import com.superteam.zzl.view.FragmentCotroller;

import com.superteam.zzl.viewmodel.ModelTestVM;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{

    public static ActivityMainBinding amb;
    private FragmentCotroller fc;
    public static Handler handler,handler1,handler2;
    public static String title1,content1;
    public static String title2,content2;
    public static String title3,content3;
    private Intent onHomeIntent;

    //退出时的时间
    private long exitTime = 0;

    /**********************测试标识**************/
    private boolean flag = true;
    /******************************************/

    @Override
    public void widgetClick(View v) {
        /***********测试底部导航栏显示与隐藏***********************/

//        if(flag) {
//            amb.rg.setVisibility(View.GONE);
//            amb.rg.setAnimation(NavigationAnimationUtils.moveToViewBottom());
//            flag = false;
//        }else{
//            amb.rg.setVisibility(View.VISIBLE);
//            amb.rg.setAnimation(NavigationAnimationUtils.moveToViewLocation());
//            flag = true;
//        }
        if(flag) {
            amb.rg.setVisibility(View.GONE);
            amb.rg.setAnimation(NavigationAnimationUtils.moveToViewBottom());
            flag = false;
        }else{
            amb.rg.setVisibility(View.VISIBLE);
            amb.rg.setAnimation(NavigationAnimationUtils.moveToViewLocation());
            flag = true;
        }
        /********************************************************/
    }
    @Override
    public ViewDataBinding initDataBinding() {
        amb = DataBindingUtil.setContentView(this,R.layout.activity_main);
        ModelTestVM modelTestVM = new ModelTestVM();
        amb.setMain(modelTestVM);
        amb.rg.setOnCheckedChangeListener(this);
        setDrawableTop();
        return amb;
    }

    /**
     * 设置RadioButton中DrawableTop的大小
     */
    private void setDrawableTop(){
        Drawable [] drawables1 = amb.rb1RecMain.getCompoundDrawables();
        Drawable [] drawables2 = amb.rb1DynMain.getCompoundDrawables();
        Drawable [] drawables3 = amb.rb1DesMain.getCompoundDrawables();
        Drawable [] drawables4 = amb.rb1MineMain.getCompoundDrawables();
        Drawable [] drawables5 = amb.rb1StoreMain.getCompoundDrawables();
        Rect r1 = new Rect(0, 0, drawables1[1].getMinimumWidth() * 2 / 3, drawables1[1].getMinimumHeight() * 2 / 3);
        drawables1[1].setBounds(r1);
        Rect r2 = new Rect(0, 0, drawables2[1].getMinimumWidth() * 2 / 3, drawables2[1].getMinimumHeight() * 2 / 3);
        drawables2[1].setBounds(r2);
        Rect r3 = new Rect(0, 0, drawables3[1].getMinimumWidth() * 2 / 3, drawables3[1].getMinimumHeight() * 2 / 3);
        drawables3[1].setBounds(r3);
        Rect r4 = new Rect(0, 0, drawables4[1].getMinimumWidth() * 2 / 3, drawables4[1].getMinimumHeight() * 2 / 3);
        drawables4[1].setBounds(r4);
        Rect r5 = new Rect(0, 0, drawables5[1].getMinimumWidth() * 2 / 3, drawables5[1].getMinimumHeight() * 2 / 3);
        drawables5[1].setBounds(r5);
        amb.rb1RecMain.setCompoundDrawables(null,drawables1[1],null,null);
        amb.rb1DynMain.setCompoundDrawables(null,drawables2[1],null,null);
        amb.rb1DesMain.setCompoundDrawables(null,drawables3[1],null,null);
        amb.rb1MineMain.setCompoundDrawables(null,drawables4[1],null,null);
        amb.rb1StoreMain.setCompoundDrawables(null,drawables5[1],null,null);

    }

    @Override
    public void initParms(Bundle parms) {
        fc = FragmentCotroller.getInstance(R.id.rl_main,this);
        fc.showFragment(0);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public void initView(View view) {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doBusiness(Context mContext) {
        handler2 = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x111) {
                    fc.showFragment(1);
                    amb.rb1DesMain.setChecked(true);
                    amb.rg.setVisibility(View.VISIBLE);
                }

            }
        };
    }

    /**
     * 点击底部导航进行相应Fragment的切换
     * @param radioGroup
     * @param i
     */



    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch(i) {
            case R.id.rb1_rec_main:
                fc.showFragment(0);
                break;
            case R.id.rb1_des_main:
                fc.showFragment(1);
                break;
            case R.id.rb1_store_main:
                fc.showFragment(2);
                break;
            case R.id.rb1_dyn_main:
                fc.showFragment(3);
                break;
            case R.id.rb1_mine_main:
                fc.showFragment(4);
                break;

        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        onHomeIntent = intent;
    }

    @Override
    protected void onResume() {
        if(onHomeIntent != null){ // home键退出后通过intent启动程序
            // dosomething···
            onHomeIntent = null;
        }

        super.onResume();

    }

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
           showToast("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }








}
